package cn.iocoder.yudao.framework.pay.core.client.impl.pax;

import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.model.PaxRequest;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.model.PaxResult;
import cn.iocoder.yudao.framework.pay.core.client.impl.pax.util.PaxHttpUtils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Objects;
import jodd.util.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * Pax支付客户端
 *
 * @author cai
 * @since 2024/1/23 8:41
 */
@Slf4j
public class DefaultPaxClient {

    private PaxClientConfig appConfig;

    public DefaultPaxClient(PaxClientConfig appConfig) {
        this.appConfig = appConfig;
    }

    @SneakyThrows
    public <T> PaxResult<T> execute(PaxRequest param, Class<T> clazz) {
        PaxResult<T> result = new PaxResult<>();
        if (!param.checkApiPath()) {
            log.info("apiPath不能为空");
            result.setCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode().toString());
            result.setMsg("apiPath不能为空");
            return result;
        }
        final String url = this.appConfig.getHost() + param.apiPath();
        final HttpResponse httpResponse = PaxHttpUtils.post(url, param, this.appConfig.getPrivateKey(), this.appConfig.getAppKey());
        if (httpResponse.isOk()) {
            String resultStr = httpResponse.body();
            log.info("pax response: {}", resultStr);
            if (StringUtil.isNotEmpty(resultStr)) {
                if (JSONUtil.isTypeJSON(resultStr)) {
                    result = JsonUtils.parseObject(resultStr, new TypeReference<PaxResult<T>>() {});
                    if (Objects.nonNull(result.getData())) {
                        result.setData(JsonUtils.parseObject(JsonUtils.toJsonString(result.getData()), clazz));
                    }
                } else {
                    log.info(resultStr);
                    result.setCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode().toString());
                    result.setMsg("接口返回值不是json");
                }
            } else {
                log.info("pax execute command {} 失败, 返回结果为空, url = {}", param.apiPath(), url);
                result.setCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode().toString());
                result.setMsg(String.format("execute command %s，请求Pax返回结果为", param.apiPath()));
            }
        } else {
            result.setCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode().toString());
            result.setMsg("接口请求失败");
        }

        return result;
    }

    public void checkSign(JSONObject obj) {
        boolean signResult = true;
        if (!signResult) {
            log.error("Pax响应结果签名校验失败, obj = {}", obj.toJSONString());
            throw new IllegalArgumentException("Pax响应结果签名校验失败");
        }
    }

}
